package hanhns.test.listview;

import hanhns.test.R;
import hanhns.test.addNote.NewNote;
import hanhns.test.database.DBAdapter;
import constant.*;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListNote extends ListActivity implements OnClickListener{
	
	private  ArrayList<Row> row_list;
	private RowAdapter adapter;
	private DBAdapter dbAdapter;
	private ListView list;
	private Button search_bt;
	private EditText search_editText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_note_test);
		
		search_bt = (Button) findViewById(R.id.button_search);
		search_bt.setOnClickListener(this);
		
		search_editText = (EditText) findViewById(R.id.search_note_edittext);
		
		
//		this.row_list = new ArrayList<Row>();
//		
//		dbAdapter = new DBAdapter(this);
//		dbAdapter.open();
//		
//		Cursor all = null;
//		all = dbAdapter.getAllNote();
//		if(all != null){
//			all.moveToFirst();
//			int title_index = all.getColumnIndex("title");
//			int content_indext = all.getColumnIndex("content");
//			while(all.moveToNext()){
//				Row r = new Row();
//				r.setNoteTitle(all.getString(title_index));
//				r.setNoteContent(all.getString(content_indext));
//				this.row_list.add(r);
//			}
//		}
//		
//		this.adapter = new RowAdapter(this, R.layout.note_row, this.row_list);
//		this.setListAdapter(adapter);
		
		
		
	}
	
	@Override
	  public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.list_menu, menu);
	    return true;
	  }

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
			case R.id.addNewNote: 
				Intent newnote = new Intent(this, NewNote.class);
				newnote.putExtra(Constant.type, Constant.add_new_note);
				startActivity(newnote);
				break;
				
			default: break;
		}
		
		return true;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
//		Toast.makeText(this, "position: " + position, Toast.LENGTH_SHORT).show();
//		Toast.makeText(this, "id: " + id, Toast.LENGTH_SHORT).show();
		
		
		TextView title_note = (TextView) v.findViewById(R.id.titleNote);
		String string_title_note = title_note.getText().toString();
		
		TextView id_note = (TextView) v.findViewById(R.id.idNote);
		int note_id = Integer.parseInt(id_note.getText().toString());
		//Toast.makeText(this, "title: " + note_lite, Toast.LENGTH_SHORT).show();
		TextView content_note = (TextView) v.findViewById(R.id.contentNote);
		String string_content_note = content_note.getText().toString();
		
		Intent update = new Intent(this, NewNote.class);
		update.putExtra(Constant.type, Constant.update_note);
		update.putExtra(Constant.title, string_title_note);
		update.putExtra(Constant.content, string_content_note);
		update.putExtra(Constant._id, note_id);
		Toast.makeText(this, "id of note click: " + note_id, Toast.LENGTH_SHORT).show();
		Log.e("note click", "id: " + note_id);
		startActivity(update);		
		super.onListItemClick(l, v, position, id);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		
		dbAdapter = new DBAdapter(this);
		dbAdapter.open();
		
		Cursor all = null;
		all = dbAdapter.getAllNote("title");
		//dbAdapter.close();
		showlist(all);
		dbAdapter.close();
//		if(all != null && all.getCount() > 0){
//			all.moveToFirst();
//			int title_index = all.getColumnIndex("title");
//			int content_indext = all.getColumnIndex("content");
//			int id_indext = all.getColumnIndex("_id");
//			do{
//					Row r = new Row();
////					Log.e("hanhns", "test test test");
//					r.setNoteTitle(all.getString(title_index));
////					Log.e("hanhns", "1: " + all.getString(title_index));
//					r.setNoteContent(all.getString(content_indext));
////					Log.e("hanhns", "2: " + all.getString(content_indext));
//					r.set_id(all.getInt(id_indext));
////					Log.e("hanhns", "3: " + all.getInt(id_indext));
//					this.row_list.add(r);
//			}
//			while(all.moveToNext());
//		}
//		
//		this.adapter = new RowAdapter(this, R.layout.note_row, this.row_list);
//		this.setListAdapter(adapter);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
			case R.id.button_search:
				String key = search_editText.getText().toString();
				key = key.trim();
				if(key.equalsIgnoreCase("")){
					Toast.makeText(this, "input key to search!", Toast.LENGTH_SHORT).show();
					dbAdapter.open();
					Cursor search_kq = dbAdapter.getAllNote("title");
					showlist(search_kq);
					dbAdapter.close();
				}else{
					Toast.makeText(this, "search for key: " + key, Toast.LENGTH_SHORT).show();
					dbAdapter.open();
					Cursor search_kq = dbAdapter.search(key);
					showlist(search_kq);
					dbAdapter.close();
					InputMethodManager imm = (InputMethodManager)getSystemService(this.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(search_editText.getWindowToken(), 0);
				}
				
				break;
			default:
				break;
		}
	}
	
	private void showlist(Cursor all){
		this.row_list = new ArrayList<Row>();
		if(all != null && all.getCount() > 0){
			all.moveToFirst();
			int title_index = all.getColumnIndex("title");
			int content_indext = all.getColumnIndex("content");
			int id_indext = all.getColumnIndex("_id");
			do{
					Row r = new Row();
//					Log.e("hanhns", "test test test");
					r.setNoteTitle(all.getString(title_index));
//					Log.e("hanhns", "1: " + all.getString(title_index));
					r.setNoteContent(all.getString(content_indext));
//					Log.e("hanhns", "2: " + all.getString(content_indext));
					r.set_id(all.getInt(id_indext));
//					Log.e("hanhns", "3: " + all.getInt(id_indext));
					this.row_list.add(r);
			}
			while(all.moveToNext());
		}
		
		this.adapter = new RowAdapter(this, R.layout.note_row, this.row_list);
		this.setListAdapter(adapter);
	}

	
}
